﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Advance.Mvc.Models;
using IdentityServer4.Models;
using IdentityServer4.Services;
using IdentityServer4.Stores;
using Microsoft.AspNetCore.Mvc;

namespace Advance.Mvc.Controllers
{
    public class ConsentController : Controller
    {
        private readonly IClientStore _clientStore;
        private readonly IIdentityServerInteractionService _identityServerInteractionService;
        private readonly IResourceStore _resourceStore;
        public ConsentController(
            IClientStore clientStore,
            IIdentityServerInteractionService identityServerInteractionService,
            IResourceStore resourceStore
            )
        {
            _clientStore = clientStore;
            _identityServerInteractionService = identityServerInteractionService;
            _resourceStore = resourceStore;
        }
        public async Task<IActionResult> Index(string returnUrl)
        {
            var request = await _identityServerInteractionService.GetAuthorizationContextAsync(returnUrl);
            if (request == null)
            {
                return BadRequest();
            }
            var client = await _clientStore.FindEnabledClientByIdAsync(request.ClientId);
            var resources = await _resourceStore.FindEnabledResourcesByScopeAsync(request.ScopesRequested);

            var vm = CreateConsentVm(client, resources);
            vm.ReturlUrl = returnUrl;
            return View(vm);
        }
        [HttpPost]
        public async Task<IActionResult> Index(InputConsentVm inputConsentVm)
        {
            ConsentResponse consentResponse = null;
            if (inputConsentVm.Button.Equals("no"))
            {
                consentResponse = ConsentResponse.Denied;
                return View();
            }
            else
            {
                var request = await _identityServerInteractionService.GetAuthorizationContextAsync(inputConsentVm.ReturlUrl);
                if (request == null)
                {
                    consentResponse = ConsentResponse.Denied;
                    return View();
                }
                consentResponse = new ConsentResponse()
                {
                    RememberConsent = inputConsentVm.RememberConsent,
                    ScopesConsented = inputConsentVm.ScopesConsented
                };
                await _identityServerInteractionService.GrantConsentAsync(request, consentResponse);
               
            }
            return Redirect(inputConsentVm.ReturlUrl);
        }

        private ConsentVm CreateConsentVm(Client client, Resources resources)
        {
            var vm = new ConsentVm();
            vm.ClientName = client.ClientName;
            vm.ClientUrl = client.ClientUri;
            vm.ClientLogoUrl = client.LogoUri;
            vm.RememberConsent = client.AllowRememberConsent;
            vm.IdentityScopes = resources.ApiResources.SelectMany(r => r.Scopes).Select(r => new ScopeVm()
            {
                Checked = r.Required,
                Name = r.Name,
                DisplayName = r.DisplayName,
                Emphasize = r.Emphasize,
                Description = r.Description,
                Required = r.Required
            });
            vm.ResourceScopes = resources.IdentityResources.Select(r => new ScopeVm()
            {
                Checked = r.Required,
                Name = r.Name,
                DisplayName = r.DisplayName,
                Emphasize = r.Emphasize,
                Description = r.Description,
                Required = r.Required
            });
            return vm;
        }
    }
}